package com.example.tiffany.noted;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends AppCompatActivity {
    private List<Notes> mlist = new ArrayList<>();
    private EditText searchData;
    private Button search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this, "663dca6db560865a269b189e82c27b37");
        initAddNote();
        initRecyclerView();
        inintSearch();
    }

    private void inintSearch() {
        searchData = findViewById(R.id.search_data);
        search = findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String data = searchData.getText().toString();
                BmobQuery<notebook> query = new BmobQuery<notebook>();
                query.addWhereEqualTo("title",data);
                query.findObjects(new FindListener<notebook>() {
                    @Override
                    public void done(List<notebook> list, BmobException e) {
                        if(e==null && list.size()>0){
                            Toast.makeText(MainActivity.this,"查询成功",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this,EditActivity.class);
                            for(notebook note : list){
                                intent.putExtra("ID",note.getObjectId());
                                intent.putExtra("TITLE",note.getTitle());
                                intent.putExtra("DATA", note.getContent());
                            }
                            startActivity(intent);
                        }else{
                            Toast.makeText(MainActivity.this,"没有这条笔记",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    private void initRecyclerView() {
        BmobQuery<notebook> notes = new BmobQuery<notebook>();
        notes.findObjects(new FindListener<notebook>() {
            @Override
            public void done(List<notebook> list, BmobException e) {
                if(e==null){
                    RecyclerView recyclerView = findViewById(R.id.recycler_view);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
                    recyclerView.setLayoutManager(layoutManager);
                    for(notebook note : list){
                        Log.e("MainActivity",note.getTitle()+";"+note.getUpdatedAt().substring(0,10));
                        String title = note.getTitle();
                        String data = note.getContent();
                        mlist.add(new Notes(title,data,note.getUpdatedAt().substring(0,10),note.getObjectId()));
                    }
                    NoteBookBaseAdapter adapter = new NoteBookBaseAdapter(MainActivity.this,mlist);
                    recyclerView.setAdapter(adapter);
                }else{
                    Log.e("MainActivity","更新内容失败");
                }
            }
        });
    }

    private void initAddNote() {
        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.addNote);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });
    }
    private long firstTime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN){
            if (System.currentTimeMillis()-firstTime>2000){
                Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                firstTime=System.currentTimeMillis();
            }else{
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
